/*  This file is part of LitSearch.

    LitSearch is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    LitSearch is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with LitSearch; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

package de.fhbrs.migration;

import java.lang.String;
import java.util.regex.Pattern;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Container for Medium
 *
 * @version $Id: Medium.java 379 2008-01-17 15:27:36Z andreas.moser.bm $
 * @author 	Andreas Moser
 * 
 * 001 10.12.2007 - Andreas Moser - Added all Standard Comments
 * 
 */

public class Medium {
	
	private int id;
	private String[] author;
	private String signatur;
	private String editor;
	private String year;
	private String title1;
	private String title2;
	private String institute;
	private String address;
	private String shelf;
	private String vol;
	private String room;
	private String remarks;
	private String page;
	private String publisher;
	private String journal;
	private String litart;
	private String isbn;
	private String issn;
	private String inventorynumber;
	
	/**
	 * Constructor. Fills only the form field width var. 
	 *
	 */
	public Medium(){
	
	}
	
	public int getId() {
		return id;
	}	
	
	public void setId(int id) {
		this.id = id;
	}
	
	/**
	 * Return sgnature
	 * @return
	 */
	public String getSignaturPrint(){
		return this.signatur;
	}
	
	public void setSignatur(String signatur) {
		this.signatur = signatur.trim();
	}
	
	/**
	 * Return the editor
	 * @return
	 */
	public String getEditorPrint() {
		return this.editor;
	}
	
	public void setEditor(String editor) {
		this.editor = editor.trim();
	}
	
	/** 
     * Returns the Year. 
     * @return Year as String 
     */
	public String getYearPrint(){
		return this.year;
	}
	
	/** 
     * Sets Year.
     * @param year as String.
     */
	public void setYear(String year) {
		this.year = year.trim();
	}
	
	/**
	 * Returns title1
	 * @return
	 */
	public String getTitle1Print(){
		return this.title1;
	}
	
	public void setTitle1(String title1) {
		
		this.title1 = title1.trim();
		
	}
	
	/**
	 * Returns titel2
	 * @return
	 */
	public String getTitle2Print(){
		return this.title2;
	}
	
	public void setTitle2(String title2) {
		this.title2 = title2.trim();
	}
	
	/**
	 * Returns the institute
	 * @return
	 */
	public String getInstitutePrint() {
		return this.institute;
	}
	
	public void setInstitute(String institute) {
		this.institute = institute.trim();
	}
	
	/**
	 * Returns the Address
	 * @return
	 */
	public String getAddressPrint() {
		return this.address;
	}
	
	public void setAddress(String address) {
		this.address = address.trim();
	}
	
	/**
	 * returns value of shelf (Schriftenreihe)
	 * @return
	 */
	public String getShelfPrint(){
		return this.shelf;
	}
	
	public void setShelf(String shelf) {
		this.shelf = shelf.trim();
	}
	
	/**
	 * Returns Volume
	 * @return
	 */
	public String getVolPrint(){
		return this.vol;
	}
	
	public void setVol(String vol) {
		this.vol = vol.trim();
	}
	
	/**
	 * returns remarks
	 * @return
	 */
	public String getRemarksPrint(){
		return this.remarks;
	}
	
	public void setRemarks(String remarks) {
		this.remarks = remarks.trim();
	}
	
	/**
	 * returns the Content of the page variable
	 * @return
	 */
	public String  getPagePrint(){
		return this.page;
	}
	
	public void setPage(String page) {
		this.page = page.trim();
	}
	
	/***
	 * Returns the kind of literature of the current media. Result depending on current language
	 * @return
	 */
	public String getLitartExport() {
		return this.litart;
		
	}
	
	public void setLitart(String litart) {
		this.litart = litart.trim();
	}

	public String[] getAuthor() {
		return author;
	}
	
	/**
	 * Return the journal
	 * @return
	 */
	public String getJournalPrint() {
		return this.journal;
	}
	
	public void setJournal(String journal) {
		if (journal.equals("clearme")) this.journal = "";
		else this.journal = journal.trim();
	}
	
	/**
	 * Return the content of publisher var
	 * @return
	 */
	public String getPublisherPrint(){
		return this.publisher;
	}
	
	public void setPublisher(String publisher) {
		this.publisher = publisher.trim();
	}
	
	/**
	 * Returns ISBN Number
	 * @return
	 */
	public String getIsbnPrint(){
		return this.isbn;
	}
	
	public void setIsbn(String isbn) {
		this.isbn = isbn.trim();
	}
	
	/**
	 * Returns ISSN Number
	 * @return
	 */
	public String getIssnPrint(){
		return this.issn;
	}
	
	public void setIssn(String issn) {
		this.issn = issn.trim();
	}
	
	/**
	 * Returns room
	 * @return
	 */
	public String getRoomPrint(){
		return this.room;
	}
	
	public void setRoom(String room) {
		if (room.equals("clearme")) this.room = "";
		else this.room = room.trim();
	}
	
	/**
	 * returns the Content of the page variable
	 * @return
	 */
	public String  getInventoryNnumberPrint(){
		return this.inventorynumber;
	}
	
	public void setInventorynumber(String in) {
		this.inventorynumber= in.trim();
	}
	
	/**
	 * sets Author Array from given Author String
	 * @param authorString
	 */
	public void setAuthorString(String authorString) {
		authorString = authorString.trim();
		this.setAuthor(convertToArray(authorString));
	}
	
	/**
	 * Converts an given String to an Array (Form of giver String: "Field on","field two","...",....
	 * @param x
	 * @return
	 */
	public String[] convertToArray(String x){	
		String regex = "\",\"";
		String [] carved = Pattern.compile(regex).split(x);
		carved[0]=carved[0].substring(1,carved[0].length());
		carved[carved.length-1]=carved[carved.length-1].substring(0,carved[carved.length-1].length()-1);
		return carved;
	}
	
	public void setAuthor(String[] author) {
		this.author = author;
	}
	
	/** 
     * Fills the Variables with data from database.
     * @param Id as an String (given as an Request Parameter from a JSP
     */
	public void initializeMedium(String idString, String hostname, String database, String username, String password) throws NoMySQLConException, SQLException, Exception {
		this.setId(Integer.parseInt(idString));
		
		MySQLConnection dbCon= MySQLConnection.getInstance(hostname,database,username,password);
		
		//Mapping MySQL Tables literature and journals
		//Because sometimes there is no match between both tables
		//we have to try out whether there is a mapping or not.
		//e.g. 'JGR' is a journal shortcut but there exists no long name 
		ResultSet res = dbCon.executeQuery("Select journal from literatur where id=" + id + ";");
		String journalShortcut = null;
		if (res != null && res.next()) {
			journalShortcut = res.getString("journal");
		}
		
		String query = "Select IF(ISNULL((SELECT `journals`.name  FROM litsearch.journals `journals` WHERE (`journals`.shortcut = '" + journalShortcut + "'))),'yes','no') as result;";
		res = dbCon.executeQuery(query);
		String yesorno = null;
		if (res != null && res.next()) {
			yesorno = res.getString("result");
		}
		
		if (yesorno.compareTo("yes")==0){
			query = "Select id, signatur, editor, year, title1, title2, institute, address, shelf, vol, remarks, page, litart, released, author, journal, publisher, isbn, issn, room, inventorynumber from literatur where id=" + id + ";";
		} else {
			query = "Select a.id, signatur, editor, year, title1, title2, institute, address, shelf, vol, remarks, page, litart, released, author, b.name as journal, publisher, isbn, issn, room, inventorynumber from literatur a, journals b where a.id= " + id + " and '" + journalShortcut + "' = b.shortcut;";
		}
		
		res = dbCon.executeQuery(query);
		
		if (res != null && res.next()) {
			this.setAuthorString( res.getString("author"));
			this.setSignatur( res.getString("signatur"));
			this.setEditor( res.getString("editor"));
			this.setYear(res.getString("year"));
			this.setTitle1(res.getString("title1"));
			this.setTitle2( res.getString("title2"));
			this.setInstitute(res.getString("institute"));
			this.setAddress(res.getString("address"));
			this.setShelf(res.getString("shelf"));
			this.setIsbn(res.getString("isbn"));
			this.setIssn(res.getString("issn"));
			this.setVol(res.getString("vol"));
			this.setRoom((res.getString("room").compareTo("0")==0) ? "" : res.getString("room"));
			this.setRemarks(res.getString("remarks"));
			this.setInventorynumber(res.getString("inventorynumber"));
			this.setPage(res.getString("page"));
			this.setLitart(res.getString("litart"));
			this.setPublisher(res.getString("publisher"));
			this.setJournal(res.getString("journal"));
		}
		
		res.close();
	}	
}
